class Solution(object):
    def totalFruit(self, fruits):
        """
        :type fruits: List[int]
        :rtype: int
        """
        d = {}
        left = 0
        right = 0
        n = len(fruits)
        max_len = 0
        while right < n:
            if fruits[right] in d:
                d[fruits[right]] = right
                max_len = max(max_len, right - left + 1)
            else:
                if len(d) < 2:
                    d[fruits[right]] = right
                    max_len = max(max_len, right - left + 1)
                else:
                    min_value = min(d.values())
                    for key in d:
                        if d[key] == min_value:
                            d_key = key
                            break
                    index = d.pop(d_key)
                    left = index + 1
                    d[fruits[right]] = right
            right += 1
        return max_len


data = Solution()
fruits = [0,1,2,2]
print(data.totalFruit(fruits))